/*
 * Copyright 2023-2025 Licensed under the Dual Licensing
 * website: https://aizuda.com
 */
package com.aizuda.bpm.beetlsql.impl;

import com.aizuda.bpm.beetlsql.mapper.FlwHisTaskMapper;
import com.aizuda.bpm.engine.dao.FlwHisTaskDao;
import com.aizuda.bpm.engine.entity.FlwHisTask;
import com.github.yitter.idgen.YitIdHelper;

import java.util.Collection;
import java.util.List;
import java.util.Optional;

/**
 * 历史任务数据访问层接口实现类
 *
 * <p>
 * <a href="https://aizuda.com">官网</a>尊重知识产权，不允许非法使用，后果自负，不允许非法使用，后果自负
 * </p>
 *
 * @author hubin
 * @since 1.0
 */
public class FlwHisTaskDaoImpl implements FlwHisTaskDao {
    private final FlwHisTaskMapper hisTaskMapper;

    public FlwHisTaskDaoImpl(FlwHisTaskMapper hisTaskMapper) {
        this.hisTaskMapper = hisTaskMapper;
    }

    @Override
    public boolean insert(FlwHisTask hisTask) {
        hisTask.setId(YitIdHelper.nextId());
        return hisTaskMapper.createQuery().insertSelective(hisTask) > 0;
    }

    @Override
    public boolean deleteById(Long id) {
        return hisTaskMapper.deleteById(id) > 0;
    }

    @Override
    public boolean deleteByInstanceIds(List<Long> instanceIds) {
        return hisTaskMapper.createLambdaQuery().andIn(FlwHisTask::getInstanceId, instanceIds).delete()>0;
    }

    @Override
    public boolean updateById(FlwHisTask hisTask) {
        return hisTaskMapper.createLambdaQuery().andEq(FlwHisTask::getId, hisTask.getId()).updateSelective(hisTask)>0;
    }

    @Override
    public FlwHisTask selectById(Long id) {
        return hisTaskMapper.single(id);
    }

    @Override
    public FlwHisTask selectStartTaskByInstanceId(Long instanceId) {
        List<FlwHisTask> hisTasks = hisTaskMapper.createLambdaQuery()
                .andEq(FlwHisTask::getInstanceId, instanceId)
                .andEq(FlwHisTask::getParentTaskId, 0).select();
        return null == hisTasks ? null : hisTasks.get(0);
    }

    @Override
    public List<FlwHisTask> selectListByInstanceIdAndTaskName(Long instanceId, String taskName) {
        return hisTaskMapper.createLambdaQuery().andEq(FlwHisTask::getInstanceId, instanceId)
                .andEq(FlwHisTask::getTaskName, taskName).select();
    }

    @Override
    public Optional<List<FlwHisTask>> selectListByInstanceId(Long instanceId) {
        return Optional.ofNullable(hisTaskMapper.createLambdaQuery().andEq(FlwHisTask::getInstanceId, instanceId).select());
    }

    @Override
    public Optional<List<FlwHisTask>> selectListByInstanceIdAndTaskState(Long instanceId, Integer taskState) {
        return Optional.ofNullable(hisTaskMapper.createLambdaQuery()
                .andEq(FlwHisTask::getInstanceId, instanceId)
                .andEq(FlwHisTask::getTaskState, taskState).select());
    }

    @Override
    public Optional<List<FlwHisTask>> selectListByInstanceIdAndTaskKey(Long instanceId, String taskKey) {
        return Optional.ofNullable(hisTaskMapper.createLambdaQuery()
                .andEq(FlwHisTask::getInstanceId, instanceId)
                .andEq(FlwHisTask::getTaskKey, taskKey)
                .desc(FlwHisTask::getCreateTime).select());
    }

    @Override
    public List<FlwHisTask> selectListByCallProcessIdAndCallInstanceId(Long callProcessId, Long callInstanceId) {
        return hisTaskMapper.createLambdaQuery().andEq(FlwHisTask::getCallProcessId, callProcessId)
                .andEq(FlwHisTask::getCallInstanceId, callInstanceId).select();
    }

    @Override
    public List<FlwHisTask> selectListByParentTaskId(Long parentTaskId) {
        return hisTaskMapper.createLambdaQuery().andEq(FlwHisTask::getParentTaskId, parentTaskId).select();
    }

    @Override
    public Collection<FlwHisTask> selectListByInstanceIdAndTaskNameAndParentTaskId(Long instanceId, String taskName, Long parentTaskId) {
        return hisTaskMapper.createLambdaQuery().andEq(FlwHisTask::getInstanceId, instanceId)
                .andEq(FlwHisTask::getTaskName, taskName)
                .andEq(FlwHisTask::getParentTaskId, parentTaskId).select();
    }
}
